package com.ywy.controller.admin;

import com.ywy.entity.Message;
import com.ywy.entity.Res;
import com.ywy.service.MessageService;
import com.ywy.service.ResService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员资源Controller
 *
 * @author ywy
 * @date 2022/9/7 14:19
 */
@Controller
@RequestMapping("admin/res")
public class AdminResController {
    @Resource
    private ResService resService;

    @Resource
    private MessageService messageService;

    /**
     * 跳转到修改资源页面
     *
     * @param id
     * @return
     */
    @RequestMapping("edit/{id}")
    @RequiresPermissions(value = {"RES_EDIT"})
    public ModelAndView edit(@PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("res", resService.getById(id));
        mav.setViewName("admin/res/edit");
        return mav;
    }

    /**
     * 跳转到资源审核页面
     *
     * @param id
     * @return
     */
    @RequestMapping("check/{id}")
    @RequiresPermissions(value = {"RES_CHECK"})
    public ModelAndView toCheckResPage(@PathVariable("id") Integer id) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("res", resService.getById(id));
        mav.setViewName("admin/res/check");
        return mav;
    }

    /**
     * 分页查询资源信息
     *
     * @param res
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    @RequiresPermissions(value = {"RES_QUERY"})
    public Map<String, Object> list(Res res, Integer page, Integer limit) {
        List<Res> resList = resService.list(res, page, limit);
        Long total = resService.getTotal(res);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("count", total);
        resultMap.put("data", resList);
        return resultMap;
    }

    /**
     * 根据id查询资源信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("getById")
    @RequiresPermissions(value = {"RES_QUERY"})
    public Map<String, Object> getById(Integer id) {
        Res res = resService.getById(id);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        resultMap.put("res", res);
        return resultMap;
    }

    /**
     * 修改资源
     *
     * @param res
     * @return
     */
    @ResponseBody
    @RequestMapping("save")
    @RequiresPermissions(value = {"RES_EDIT"})
    public Map<String, Object> save(Res res) {
        Res oldRes = resService.getById(res.getId());
        oldRes.setName(res.getName());
        oldRes.setResType(res.getResType());
        oldRes.setContent(res.getContent());
        oldRes.setDownloadUrl(res.getDownloadUrl());
        oldRes.setDownloadPass(res.getDownloadPass());
        oldRes.setPoints(res.getPoints());
        resService.save(oldRes);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 修改审核状态
     *
     * @param res
     * @return
     */
    @ResponseBody
    @RequestMapping("updateStatus")
    @RequiresPermissions(value = {"UPDATE_CHECK_STATUS"})
    public Map<String, Object> updateStatus(Res res) {
        Res oldRes = resService.getById(res.getId());

        Message message = new Message();
        message.setUser(oldRes.getUser());
        message.setPublishDate(new Date());
        String content;
        if (res.getCheckStatus() == 2) {
            oldRes.setCheckStatus(2);

            content = "【审核通过】您发布的【"+oldRes.getName()+"】资源审核成功！";
        } else {
            oldRes.setCheckStatus(3);
            oldRes.setReason(res.getReason());

            content = "【审核失败】您发布的【"+oldRes.getName()+"】帖子审核未成功，原因是："+res.getReason();
        }
        // 修改资源审核状态
        resService.save(oldRes);

        message.setContent(content);
        // 创建系统消息
        messageService.save(message);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 修改热门状态
     *
     * @param res
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("updateHotStatus")
    @RequiresPermissions(value = {"UPDATE_HOT_STATUS"})
    public Map<String, Object> updateHotStatus(Res res) {
        Res oldRes = resService.getById(res.getId());
        oldRes.setHot(res.isHot());
        resService.save(oldRes);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 根据id删除资源
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    @RequiresPermissions(value = {"RES_DELETE"})
    public Map<String, Object> delete(Integer id) {
        // 删除资源信息
        resService.delete(id);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 批量删除资源
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteBatch")
    @RequiresPermissions(value = {"RES_DELETE"})
    public Map<String, Object> deleteBatch(String ids) {
        String[] idsStr = ids.split(",");
        for (int i = 0; i < idsStr.length; i++) {
            // 删除资源信息
            resService.delete(Integer.parseInt(idsStr[i]));
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 生成所有资源索引
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "genAllIndex")
    @RequiresPermissions(value = {"GEN_RES_INDEX"})
    public boolean genAllIndex() {
        return resService.genAllIndex();
    }
}
